using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using log4net;

namespace DailyReport.Core.Data.Dao.DaoFactory
{
    public class DaoFactory
    {
        public static ILog log = LogManager.GetLogger("DaoFactory");
        public static DaoFactory i=new DaoFactory();
        private Dictionary<string, string> daoDefinitions;
        private DaoFactory()
        {
            //readDaoDefinitions();
        }


        private DaoFactory(Dictionary<string, string> daoDefinitions)
        {
            this.daoDefinitions = daoDefinitions;
        }

        /// <summary>
        /// Read the definitions of the xml file
        /// </summary>
        private void readDaoDefinitions()
        {
            XMLReader xmlReader=new XMLReader();
            daoDefinitions = xmlReader.readXml(@"C:\As\dotNET\Radamantis\radamantis\DailyReport\src\DailyReport.Core\Data\Dao\DaoFactory\DaoDefinitions.xml");
        }
        
        public static void initialize(XmlNode node)
        {
            XMLReader xmlReader = new XMLReader();
            i = new DaoFactory(xmlReader.readXml(node));
        }
        
        /// <summary>
        /// Returns an instance of the required Dao
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public object getDaoInstance(string name)
        {
            
            string className = null;
            object o = null;
            if (daoDefinitions.ContainsKey(name))
                className= daoDefinitions[name];

            //log.Debug("NAME:"+ name+ "EL TIOP: " + className);
            //log.Debug(daoDefinitions);
            if (className != null)
            {
                Type t = Type.GetType(className);
                //log.Debug("EL TIPO: " + name + " ES IGUAL A: " + t.ToString());
                o = Activator.CreateInstance(t);
            }
            return (o);
        }
    }
}
